package com.sdp.right.control;


import java.time.LocalDateTime;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import com.sdp.core.bizc.db.Query;
import com.sdp.core.bizc.vo.DataCenter;
import com.sdp.core.bizc.vo.Result;
import com.sdp.right.db.sys_login_log.service.ISysLoginLogService;
import com.sdp.right.entity.SysEmp;
import com.sdp.right.entity.SysLoginLog;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author zsp
 * @since 2024-05-15
 */
@RestController
@RequestMapping("/SysLoginLogController")
public class SysLoginLogController {

	@Autowired
	private ISysLoginLogService iSysLoginLogService;
	
	@Autowired
	private Query query;
	
	@PostMapping(value = "/page")
	public Result<?> page(@RequestBody DataCenter dataCenter) {
		return Result.ok(query.page(iSysLoginLogService, SysLoginLog.class, dataCenter));
	}
	
	@Transactional(rollbackFor=Exception.class)
	public void addLoginLog(SysEmp sysEmp) {
		RequestAttributes requestAttributes = RequestContextHolder.getRequestAttributes();
		HttpServletRequest request = (HttpServletRequest) requestAttributes.resolveReference(RequestAttributes.REFERENCE_REQUEST);
		SysLoginLog loginLog = new SysLoginLog();
		loginLog.setLoginIp(getIpAddress(request));
		loginLog.setLoginAccountNo(sysEmp.getAccountNo());
		loginLog.setLoginEmpName(sysEmp.getEmpName());
		loginLog.setLoginEmpNo(sysEmp.getEmpNo());
		loginLog.setLoginTime(LocalDateTime.now());
		iSysLoginLogService.save(loginLog);
	}
	
	@GetMapping(value="batchDel")
	@Transactional(rollbackFor=Exception.class)
	public Result<?> batchDel(@RequestParam(name="keyIds",required=true) List<String> keyIds) {
		if(keyIds==null || keyIds.size()==0) {
			return Result.error("请先选择要删除的记录");
		}
		iSysLoginLogService.removeByIds(keyIds);
		return Result.ok();
	}
	
	/**
     * 获取真实IP
     * @param request
     * @return
     */
	private static String getIpAddress(HttpServletRequest request) {
        String Xip = request.getHeader("X-Real-IP");
        String XFor = request.getHeader("X-Forwarded-For");
        if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
            //多次反向代理后会有多个ip值，第一个ip才是真实ip
            int index = XFor.indexOf(",");
            if(index != -1){
                return XFor.substring(0,index);
            }else{
                return XFor;
            }
        }
        XFor = Xip;
        if(StringUtils.isNotEmpty(XFor) && !"unKnown".equalsIgnoreCase(XFor)){
            return XFor;
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("Proxy-Client-IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("WL-Proxy-Client-IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_CLIENT_IP");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getHeader("HTTP_X_FORWARDED_FOR");
        }
        if (StringUtils.isBlank(XFor) || "unknown".equalsIgnoreCase(XFor)) {
            XFor = request.getRemoteAddr();
        }
        return XFor;
    }
}

